package org.example.ui.administrator;
import org.example.business.UserManageBusiness;
import org.example.manage.UserManage;
import org.example.model.User;
import org.example.ui.PromptComboBox;
import org.example.ui.SearchField;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.*;
import java.util.List;

public class ManageUserPanel extends JPanel {
    private JLabel[] topLabel = new JLabel[7];
    private SpringLayout layout = new SpringLayout();
    private static SpringLayout userLayout = new SpringLayout();
    private JCheckBox box;
    private static JPanel userPanel = new JPanel();
    private SearchField search;
    private PromptComboBox comboBox = new PromptComboBox(1);
    private String[] topText = {"       编号        ", "         头像        ",
            "                    昵称                  ", "                      邮箱                   ", "           身份       ", "          状态        ",
            "     操作     "};
    private static List<User> list;
    public ManageUserPanel() {
        setLayout(layout);
        search = new SearchField();
        search.setFont(new Font("", Font.BOLD, 15));
        JTable table = new JTable();
        TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
        box = (JCheckBox) renderer;
        Image image2 = new ImageIcon("background/bag_icon.jpg").getImage().
                getScaledInstance(10, 10, Image.SCALE_DEFAULT);
        box.setIcon(new ImageIcon(image2));
        box.setSelectedIcon(new ImageIcon(image2));
        add(box);
        userPanel.setLayout(userLayout);
        setBorder(BorderFactory.createLoweredSoftBevelBorder());
        JScrollPane scrollPane = new JScrollPane(userPanel);
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        userPanel.setBackground(null);
        userPanel.setOpaque(false);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.getVerticalScrollBar().setUnitIncrement(20);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        add(scrollPane);
        add(search);
        add(comboBox);
        layout.putConstraint(SpringLayout.WEST, search, 10, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, search, 10, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.EAST, search,-250, SpringLayout.HORIZONTAL_CENTER,this);
        layout.putConstraint(SpringLayout.SOUTH, search, 50, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, comboBox, 10, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, comboBox, 0, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.SOUTH, comboBox, 0, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.EAST, comboBox, 0, SpringLayout.EAST, search);
        layout.putConstraint(SpringLayout.WEST, box, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, box, 100, SpringLayout.SOUTH, search );
        Font font = new Font("", Font.BOLD, 15);
        for (int i = 0; i < 7; i++) {
            topLabel[i] = new JLabel(topText[i], JLabel.CENTER);
            topLabel[i].setFont(font);
            topLabel[i].setOpaque(true);
            topLabel[i].setBackground(Color.WHITE);
            topLabel[i].setBorder(null);
            add(topLabel[i]);
            if (i == 0) {
                layout.putConstraint(SpringLayout.WEST, topLabel[i], 2, SpringLayout.EAST, box);
                layout.putConstraint(SpringLayout.NORTH, topLabel[i], 100, SpringLayout.SOUTH, search);
            }
            else {
                layout.putConstraint(SpringLayout.WEST, topLabel[i], 0, SpringLayout.EAST, topLabel[i-1]);
                layout.putConstraint(SpringLayout.NORTH, topLabel[i], 100, SpringLayout.SOUTH, search);
            }
            if (i == 6) {
                layout.putConstraint(SpringLayout.EAST, topLabel[i], 0, SpringLayout.EAST, this);
            }
        }
        UserManageBusiness.userPanel = userPanel;
        UserManageBusiness.userLayout = userLayout;
        UserManageBusiness.setUserItems();
        setAllListener();
        layout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, scrollPane, 10 ,SpringLayout.SOUTH, topLabel[2]);
        layout.putConstraint(SpringLayout.SOUTH, scrollPane, -10, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.EAST , scrollPane, 0, SpringLayout.EAST, this);
        setVisible(true);
    }
    private void setAllListener() {
        search.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() != KeyEvent.VK_ENTER) {
                    comboBox.update(search.getText());
                    comboBox.hidePopup();
                    comboBox.showPopup();
                }
                if (search.getText().isEmpty()) {
                    UserManageBusiness.setUserItems();
                    search.requestFocus();
                }
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    userPanel.removeAll();
                    boolean b = true;
                    int num = 1;
                    ManageUserItem userItem = null;
                    for (User user : UserManageBusiness.list) {
                        if (user.mailbox.contains(search.getText())) {
                            ManageUserItem item = new ManageUserItem(user, num);
                            userPanel.add(item);
                            if (b) {
                                b = false;
                                userLayout.putConstraint(SpringLayout.NORTH, item, 0, SpringLayout.NORTH, userPanel);
                                userLayout.putConstraint(SpringLayout.SOUTH, item, 100, SpringLayout.NORTH, userPanel);
                                userItem = item;
                            } else {
                                userLayout.putConstraint(SpringLayout.NORTH, item, 1, SpringLayout.SOUTH, userItem);
                                userLayout.putConstraint(SpringLayout.SOUTH, item, 100, SpringLayout.SOUTH, userItem);
                                userItem = item;
                            }
                            num ++;
                            userLayout.putConstraint(SpringLayout.WEST, item, 0, SpringLayout.WEST, userPanel);
                            userLayout.putConstraint(SpringLayout.EAST, item, 0, SpringLayout.EAST, userPanel);
                        }
                    }
                    userPanel.setVisible(false);
                    userPanel.setVisible(true);
                    search.requestFocus();
                }
            }
        });
        comboBox.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getModifiers() == 16)
                    search.setText((String) comboBox.getSelectedItem());
            }
        });
    }
}
